package com.woniu.controller.contemtService;

import com.baomidou.mybatisplus.core.metadata.IPage;


import com.woniu.model.contentMService.entity.ColumnM;
import com.woniu.model.gh.common.Result;
import com.woniu.service.contentService.IColumnMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/columnM")
public class ColumnMController {

    @Autowired
    private IColumnMService columnMService;

    @GetMapping("/findAll")
    public Result<List<ColumnM>> findAll() {
        List<ColumnM> list = columnMService.list();
        return Result.ok(list);
    }

    //分页
    @PostMapping("/CounM")
    public Result list(@RequestBody Map<String,Object> condition) {
        Integer pageNum = (Integer) condition.get("pageNum");
        Integer pageSize = (Integer) condition.get("pageSize");
        IPage<ColumnM> page = columnMService.findByType(pageNum, pageSize);
        return Result.ok(page);
    }
    //删除
    @RequestMapping("/deleteById")
    public Result deleteById(Long id) {
        int row = columnMService.deleteById(id);
        return Result.ok(row);
    }

    /**
     * 添加或修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ColumnM columnM) {
        boolean update = columnMService.saveOrUpdate(columnM);
        return Result.ok(update);
    }

    @PostMapping("/add")
    public Result save(@RequestBody ColumnM columnM) {
       columnMService.save(columnM);
        return Result.ok();
    }

    @PostMapping("/changeBoole")
    public Result changeBoole(@RequestBody ColumnM columnM) {
        if (columnM.getStatus() == null){
            columnM.setStatus(1);
        }
       else if (columnM.getStatus() == 0)
            columnM.setStatus(1);
        else columnM.setStatus(0);
        columnMService.updateById(columnM);
        return Result.ok();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean id = columnMService.removeByIds(ids);
        return Result.ok(id);
    }


}